


#import "MultiColumnTableViewViewController.h"
#import "NSObject+DelayedBlock.h"
#import "Car.h"
#define ROWS 100

@interface MultiColumnTableViewViewController()
@property (strong,nonatomic)NSMutableArray *myData;
- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer;

@end





@implementation MultiColumnTableViewViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        srand(time(0));
    }
                 
    return self;
}


- (void)dealloc
{
    [data release];
    [tblView release];
    [sectionHeaderData release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    numberOfColumns = 5;
    numberOfSections = 5;
    
    int sectionDistro[] = {5, 7, 4, 9, 2};
    colWidth = 240.0f;
    
    data = [[NSMutableArray alloc] initWithCapacity:numberOfSections * 5];
    sectionHeaderData = [[NSMutableArray alloc] initWithCapacity:numberOfSections];
    
    for (int i = 0; i < numberOfSections; i++) {
        
        int rows = sectionDistro[i];
        NSMutableArray *a = [NSMutableArray arrayWithCapacity:numberOfColumns];
        for (int j = 0; j < numberOfColumns; j++) {
            
            int d = rand() % 100;
            
            NSMutableString *text = [NSMutableString stringWithFormat:@"S %d C %d", i, j];
            if (d < 66) {
                [text appendFormat:@"\nsecond line"];
            }
            
            if (d < 33) {
                [text appendFormat:@"\nthird line"];
            }
            
            
            [a addObject:text];
        }
        [sectionHeaderData addObject:a];

        NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:10];
        for (int k = 0; k < rows; k++) {
            
            NSMutableArray *rowArray = [NSMutableArray arrayWithCapacity:numberOfColumns];
            for (int j = 0; j < numberOfColumns; j++) {
                int d = rand() % 100;
                
                NSMutableString *text = [NSMutableString stringWithFormat:@"(%d, %d, %d)", i, k, j];
                if (d < 66) {
                    [text appendFormat:@"\nsecond line"];
                }
                
                if (d < 33) {
                    [text appendFormat:@"\nthird line"];
                }
                
                [rowArray addObject:text];
            }
            
            [sectionArray addObject:rowArray];
        }
        
        [data addObject:sectionArray];
    }
    
    
    tblView = [[EWMultiColumnTableView alloc] initWithFrame:CGRectInset(self.view.bounds, 5.0f, 5.0f)];
    
    tblView.sectionHeaderEnabled = YES;
    
    tblView.boldSeperatorLineColor = [UIColor blackColor];
    tblView.normalSeperatorLineColor = [UIColor grayColor];
//    tblView.boldSeperatorLineWidth = 10.0f;
//    tblView.normalSeperatorLineWidth = 10.0f;
    tblView.dataSource = self;
    tblView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:tblView];
    
//    [self performBlock:^{
//            
//        [tblView scrollToColumn:1 position:EWMultiColumnTableViewColumnPositionMiddle animated:YES];
//        
//    } afterDelay:0.5];
    
    
    
    [self initData];
    
    
}

-(void)initData{
    NSMutableDictionary *jbcs = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"28.30万",@"厂商指导价",@"中型车",@"级别", nil];
    
    NSMutableDictionary *cs = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"4624",@"长度(mm)",@"1811",@"宽度(mm)",@"1454",@"高度(mm)", nil];
    
    
     NSMutableDictionary *bsx = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"8档手自一体",@"简称",@"8",@"挡位个数",@"自动变速箱",@"变速箱类型",@"这是一个初步的demo",@"备注", nil];
    NSMutableDictionary *clzd = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"通风盘式",@"前制动类型",@"手刹",@"驻车制动类型",@"205/60 R16",@"前轮胎规格",@"205/60R16",@"后轮胎规格", nil];
  
    
    Car *car1 = [[Car alloc]initWithName:@"宝马3系\n2013款316i进取型" JBCS:jbcs CS:cs BSX:bsx CLZD:clzd];
    
    
    NSMutableDictionary *jbcs2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"49.96万",@"厂商指导价",@"中大型车",@"级别", nil];
    
    NSMutableDictionary *cs2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"5047",@"长度(mm)",@"1860",@"宽度(mm)",@"1491",@"高度(mm)", nil];
    
    NSMutableDictionary *bsx2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"8档手自一体",@"简称",@"8",@"挡位个数",@"自动变速箱",@"变速箱类型",@"这是第二个车型的demo",@"备注", nil];
    NSMutableDictionary *clzd2 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"通风盘式",@"前制动类型",@"手刹",@"驻车制动类型",@"245/45 R18",@"前轮胎规格",@"245/45 R18",@"后轮胎规格", nil];
    Car *car2 = [[Car alloc]initWithName:@"宝马5系\n2014款525Li豪华设计套装" JBCS:jbcs2 CS:cs2 BSX:bsx2 CLZD:clzd2];
    
    
    Car *car3 = [[Car alloc]initWithName:@"宝马3系\n2014款Demo豪华设计套装" JBCS:jbcs2 CS:cs2 BSX:bsx2 CLZD:clzd2];
    
    
   
    Car *car4 = [[Car alloc]initWithName:@"宝马008系\n2014款Demo豪华设计套装" JBCS:jbcs2 CS:cs2 BSX:bsx2 CLZD:clzd2];
    
    
 
    Car *car5 = [[Car alloc]initWithName:@"宝马009系\n2014款Demo豪华设计套装" JBCS:jbcs2 CS:cs2 BSX:bsx2 CLZD:clzd2];
    
    
    self.myData  = [[NSMutableArray alloc]init];
    [self.myData addObject:car1];
    [self.myData addObject:car2];
    [self.myData addObject:car3];
    [self.myData addObject:car4];
    [self.myData addObject:car5];
    [tblView reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    
    // e.g. self.myOutlet = nil;
    [tblView release];
    tblView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

#pragma mark - EWMultiColumnTableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(EWMultiColumnTableView *)tableView
{
    if ([_myData count]==0) {
        return 0;
    }else{
        Car *car  = _myData[0];
        
        return [car.totalCSName count];
    }
    
}

- (UIView *)tableView:(EWMultiColumnTableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath column:(NSInteger)col
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(50, 15.0f, 250, 40.0f)] autorelease];
    l.backgroundColor = [UIColor clearColor];
    l.textAlignment = NSTextAlignmentCenter;
    l.numberOfLines = 0;
    l.lineBreakMode = NSLineBreakByCharWrapping;
    
    return l;
    
    
}


- (void)tableView:(EWMultiColumnTableView *)tableView setContentForCell:(UIView *)cell indexPath:(NSIndexPath *)indexPath column:(NSInteger)col{
    UILabel *l = (UILabel *)cell;
    Car *car =  _myData[col];
    NSString *key = car.totalCSName[indexPath.section][indexPath.row];
    l.text = [car.totalCSDict[indexPath.section]objectForKey:key];
    
    CGRect f = l.frame;
    f.size.width = [self tableView:tableView widthForColumn:col];
    l.frame = f;
    
    [l sizeToFit];
}

- (CGFloat)tableView:(EWMultiColumnTableView *)tableView heightForCellAtIndexPath:(NSIndexPath *)indexPath column:(NSInteger)col
{
    NSString *str = [[[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:col];
    CGSize s = [str sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]
               constrainedToSize:CGSizeMake([self tableView:tableView widthForColumn:col], MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    
    return s.height + 20.0f;
}

- (CGFloat)tableView:(EWMultiColumnTableView *)tableView widthForColumn:(NSInteger)column
{
    return colWidth;
}

- (NSInteger)tableView:(EWMultiColumnTableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Car *car = _myData[0];
    
    return [[car.totalCSName objectAtIndex:section] count];
    
}

- (UIView *)tableView:(EWMultiColumnTableView *)tableView sectionHeaderCellForSection:(NSInteger)section column:(NSInteger)col
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [self tableView:tableView widthForColumn:col], 40.0f)] autorelease];
    l.backgroundColor = [UIColor clearColor];
    return l;
}

- (void)tableView:(EWMultiColumnTableView *)tableView setContentForSectionHeaderCell:(UIView *)cell section:(NSInteger)section column:(NSInteger)col
{
    
    UILabel *l = (UILabel *)cell;
    
    
    
    cell.superview.backgroundColor = [UIColor yellowColor];
    
    CGRect f = l.frame;
    f.size.width = [self tableView:tableView widthForColumn:col];
    l.frame = f;
    
    [l sizeToFit];
}

- (NSInteger)numberOfColumnsInTableView:(EWMultiColumnTableView *)tableView
{
    return [_myData count];
}

#pragma mark Header Cell

- (UIView *)tableView:(EWMultiColumnTableView *)tableView headerCellForIndexPath:(NSIndexPath *)indexPath
{
    UILabel *label =[[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100, 50.0f)] autorelease];
    label.backgroundColor = [UIColor clearColor];
    return label;
    
    
}

- (void)tableView:(EWMultiColumnTableView *)tableView setContentForHeaderCell:(UIView *)cell atIndexPath:(NSIndexPath *)indexPath
{
    UILabel *l = (UILabel *)cell;
    Car *car = _myData[0];
    l.text = car.totalCSName[indexPath.section][indexPath.row];
    
}

//- (CGFloat)tableView:(EWMultiColumnTableView *)tableView heightForHeaderCellAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 30;
//}

//- (CGFloat)tableView:(EWMultiColumnTableView *)tableView heightForSectionHeaderCellAtSection:(NSInteger)section column:(NSInteger)col
//{
//    return 30;
//}

- (UIView *)tableView:(EWMultiColumnTableView *)tableView headerCellInSectionHeaderForSection:(NSInteger)section
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [self widthForHeaderCellOfTableView:tableView], 30.0f)] autorelease];
    
        l.backgroundColor = [UIColor yellowColor];
    
    
    return l;

}


- (void)tableView:(EWMultiColumnTableView *)tableView setContentForHeaderCellInSectionHeader:(UIView *)cell AtSection:(NSInteger)section
{
    UILabel *l = (UILabel *)cell;
    Car *car = _myData[0];
    l.text = car.bigLeavel[section];
    
    
}

- (CGFloat)widthForHeaderCellOfTableView:(EWMultiColumnTableView *)tableView
{
    return 150;
}


- (UIView *)tableView:(EWMultiColumnTableView *)tableView headerCellForColumn:(NSInteger)col
{
    UILabel *l =  [[[UILabel alloc] initWithFrame:CGRectMake(0, 0.0f, 250, 100)] autorelease];
    l.numberOfLines = 0;
    l.lineBreakMode = NSLineBreakByCharWrapping;
    Car *car  = _myData[col];
    l.text = car.name;
    
    l.userInteractionEnabled = YES;
    l.textAlignment = NSTextAlignmentCenter;
    l.backgroundColor = [UIColor clearColor];
    l.tag = col;
    UITapGestureRecognizer *recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)] autorelease];
    recognizer.numberOfTapsRequired = 2;
    [l addGestureRecognizer:recognizer];

    return l;
}

- (UIView *)topleftHeaderCellOfTableView:(EWMultiColumnTableView *)tableView
{
    UILabel *l =  [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 250.0f, [self heightForHeaderCellOfTableView:tableView])] autorelease];
    l.backgroundColor = [UIColor whiteColor];
    l.text = @"车型名称";
    
    return l;
}

- (CGFloat)heightForHeaderCellOfTableView:(EWMultiColumnTableView *)tableView
{
    return 100.0f;
}

- (void)tableView:(EWMultiColumnTableView *)tableView swapDataOfColumn:(NSInteger)col1 andColumn:(NSInteger)col2
{
    for (int i = 0; i < [self numberOfSectionsInTableView:tableView]; i++) {
        NSMutableArray *section = [data objectAtIndex:i];
        for (int j = 0; j < [self tableView:tableView numberOfRowsInSection:i]; j++) {
            NSMutableArray *a = [section objectAtIndex:j];
            id tmp = [[a objectAtIndex:col2] retain];
            
            [a replaceObjectAtIndex:col2 withObject:[a objectAtIndex:col1]];
            [a replaceObjectAtIndex:col1 withObject:tmp];
            [tmp release];
        }
    }
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer
{
    int col = [recognizer.view tag];
    [_myData removeObjectAtIndex:col];
    
    [tblView reloadData];

}

@end
